var _datasDatagrid,selectForm,createDate1,createDate2,checkName,checkBtid,checkStatus;
var dialogWeixinUser,formWeixinUser,remark,dialogWeixinGroup,weixinGroup;

function syncUser() {
	$.post(sy.bp()+"/admin/weixin/user/sync", {}, function(data) {
		if (data && data.result==1) {
			$.messager.show({msg:data.msg,title:'系统提示'});
			_datasDatagrid.datagrid('unselectAll');
			_datasDatagrid.datagrid('reload');
		} else {
			$.messager.alert('警告', data.msg, 'warning');
		}
	}, "json");
}

function updateUser() {
	var rows = _datasDatagrid.datagrid('getSelections');
	if (rows.length > 0) {
		$.messager.confirm('请确认', '您确实要更新当前的用户信息码?', function(b){
			if (b) {
				var ids = [];
				for ( var i = 0; i < rows.length; i++) {
					ids.push(rows[i].openid);
				}
				$.post(sy.bp()+"/admin/weixin/user/updateBatch", {ids:ids.join(",")}, function(data) {
					if (data && data.result==1) {
						$.messager.show({msg:data.msg,title:'系统提示'});
						_datasDatagrid.datagrid('unselectAll');
						_datasDatagrid.datagrid('reload');
					} else {
						$.messager.alert('警告', data.msg, 'warning');
					}
				}, "json");
			}
		});
	} else {
		$.messager.alert('提示', '请选择要删除的记录', 'error');
	}
};

function updateBatchmoveGroup() {
	var rows = _datasDatagrid.datagrid('getSelections');
	if (rows.length > 0) {
		dialogWeixinGroup.dialog('open');
	} else {
		$.messager.alert('提示', '请选择要删除的记录', 'error');
	}
};

function moveUser() {
	var rows = _datasDatagrid.datagrid('getSelections');
	var ids = [];
	for ( var i = 0; i < rows.length; i++) {
		ids.push(rows[i].openid);
	}
	var groupid = weixinGroup.combobox('getValue');
	$.post(sy.bp()+"/admin/weixin/user/updateBatchmoveGroup", {openid_list:ids.join(","),to_groupid:groupid}, function(data) {
		if (data && data.result==1) {
			$.messager.show({msg:data.msg,title:'系统提示'});
			dialogWeixinGroup.dialog('close');
			_datasDatagrid.datagrid('unselectAll');
			_datasDatagrid.datagrid('reload');
		} else {
			$.messager.alert('警告', data.msg, 'warning');
		}
	}, "json");
}

function updateRemarkUser() {
	if (formWeixinUser.form('validate')) {
		$(btnUpdateRemark).linkbutton('disable');
		var openid = $('#openid').text();
		$.post(sy.bp()+"/admin/weixin/user/updateRemark", {openid:openid,remark:remark.val()}, function(data) {
			if (data && data.result==1) {
				$.messager.show({msg:data.msg,title:'系统提示'});
				_datasDatagrid.datagrid('unselectAll');
				_datasDatagrid.datagrid('reload');
				dialogWeixinUser.dialog('close');
			} else {
				$.messager.alert('警告', data.msg, 'warning');
			}
		}, "json");
	}
}

function search_datas() {
	_datasDatagrid.datagrid('load',sy.serializeObject(selectForm));
}

function reset_datas() {
	createDate1.datebox('setValue', '');
	createDate2.datebox('setValue', '');
	checkBtid.combobox('setValue', '');
	checkStatus.combobox('setValue', '');
	checkName.val('');
	_datasDatagrid.datagrid('load',{});
}

$(function() {
	_datasDatagrid = $('#_datasDatagrid');
	selectForm = $('#selectForm').form({});
	dialogWeixinUser = $('#dialogWeixinUser');
	formWeixinUser = $('#formWeixinUser').form({});
	remark = $('#remark').validatebox({validType:['unnormal','maxLength[25]']});
	dialogWeixinGroup = $('#dialogWeixinGroup');
	dialogWeixinUser.show().dialog({title:'用户详情',iconCls:'icon-bug_delete',resizable:false,closed:true,closable:true,modal:true,
		buttons:[{id:'btnUpdateRemark',text:'修改备注',handler:updateRemarkUser}]});
	dialogWeixinGroup.show().dialog({title:'请选择要移动到的分组',iconCls:'icon-bug_delete',resizable:false,closed:true,closable:true,modal:true,
		buttons:[{text:'移动',handler:moveUser}]});
	weixinGroup = $('#weixinGroup').combobox({required:true,editable:false,valueField:'id',textField:'name',url:sy.bp()+"/admin/weixin/group/listAll"}).combobox('activeTextArrow').combobox('setValue', 0);
	
	_datasDatagrid.datagrid({
		url : sy.bp()+'/admin/weixin/user/list',
		fit : true,
		autoRowHeight : true,
		striped : true,
		rownumbers : true,
		pagination : true,
		singleSelect : false,
		fitColumns : true,
		pageSize : 10,
		pageList : [10,20,30,40,50],
		nowrap : true,
		border : false,
		idField : 'id',
		sortName : 'subscribe_time',
		sortOrder : 'desc',
		view: sy.myview,
        emptyMsg: '没有查询到相关数据',
		toolbar: [
		          {text:'刷新',iconCls: 'icon-reload',handler: function(){_datasDatagrid.datagrid('reload');}},'-',
		          {text:'获取用户',iconCls: 'icon-reload',handler: syncUser},'-',
		          {text:'更新用户',iconCls: 'icon-reload',handler: updateUser},'-',
		          {text:'变更分组',iconCls: 'icon-reload',handler: updateBatchmoveGroup},'-'
		         ],
		columns:[[
		          {field:'id',title:'编号',align:'center',width:100,sortable:true,checkbox:true},
		          {field:'nickname',title:'昵称',align:'left',width:200,sortable:true},
		          {field:'sex',title:'性别',align:'center',width:100,sortable:true},
		          {field:'subscribe',title:'是否关注',align:'center',width:100,sortable:true},
		          {field:'subscribe_time',title:'关注时间',align:'center',width:200,sortable:true},
		          //{field:'openid',title:'用户id',align:'left',width:100,sortable:true},
		          //{field:'country',title:'国家',align:'left',width:100,sortable:true},
		          //{field:'province',title:'省份',align:'left',width:100,sortable:true},
		          {field:'city',title:'城市',align:'left',width:100,sortable:true},
		          //{field:'language',title:'语言',align:'left',width:100,sortable:true},
		          //{field:'headimgurl',title:'头像',align:'left',width:100,sortable:true},
		          //{field:'unionid',title:'unionid',align:'left',width:100,sortable:true},
		          //{field:'groupid',title:'所属组',align:'left',width:100,sortable:true},
		          {field:'remark',title:'备注',align:'left',width:300,sortable:true},
		          {field:'groupname',title:'所属组',align:'left',width:100}
				]],
		onLoadSuccess : function(data) {
			$.messager.progress('close');
		},
		onDblClickRow : function(rowIndex, rowData) {
			$('#id').val(rowData.id);
			$('#subscribe').text(rowData.subscribe);
			$('#openid').text(rowData.openid);
			$('#nickname').text(rowData.nickname);
			$('#sex').text(rowData.sex);
			$('#country').text(rowData.country);
			$('#province').text(rowData.province);
			$('#city').text(rowData.city);
			$('#language').text(rowData.language);
			$('#headimgurl').attr("src",rowData.headimgurl);
			$('#subscribe_time').text(rowData.subscribe_time);
			$('#unionid').text(rowData.unionid);
			remark.val(rowData.remark);
			$('#groupid').text(rowData.groupid);
			$(btnUpdateRemark).linkbutton('enable');
			dialogWeixinUser.dialog('open');
		}
	});
});